#include "stdafx.h"

int EnemyMgr::num = 0;

EnemyMgr::EnemyMgr()
{
	/*std::wstring prefix = L"enemy";
	for (wchar_t i = '1'; i < '1' + MAX; i++)
	{
		create(prefix + i, 800, 300, 100, 10);
	}*/
}

EnemyMgr::~EnemyMgr()
{
	clear();
}

EnemyMgr * EnemyMgr::getInstance()
{
	static EnemyMgr manager;

	return &manager;
}

//void EnemyMgr::create(const std::wstring & name, double x, double y, int v, int life, const std::wstring & bname, const tagBullet & bullet)
//{
//	enemies.push_back(new EnemyTank(name, x, y, DIR_DOWN, v, life, bname, bullet));
//}

void EnemyMgr::create(const tagEnemy & enemy, double x, double y)
{
	enemies.push_back(new EnemyTank(enemy, x, y));
	num++;
}

void EnemyMgr::update(double dtime)
{
	time += dtime;

	//if (move)
	//{
		std::vector<EnemyTank *>::iterator e = enemies.begin();

		while (e != enemies.end())
		{
			if (move)
			{
				(*e)->update(dtime);
			}
			else
			{
				if (time >= stoptime)
				{
					move = true;
				}
			}
			
			if ((*e)->getLife() <= 0)
			{
				delete *e;
				e = enemies.erase(e);
				num--;
			}
			else
			{
				e++;
			}
		}
	//}
	//else
	//{

	//	if (time >= stoptime)
	//	{
	//		move = true;
	//	}
	//}
}

void EnemyMgr::draw(Graphics * graphics)
{
	for (EnemyTank * e : enemies)
	{
		e->draw(graphics);
	}
}

EnemyTank * EnemyMgr::collide(Point pt)
{
	for (EnemyTank * e : enemies)
	{
		if (e->collide(pt))
		{
			return e;
		}
	}

	return nullptr;
}

EnemyTank * EnemyMgr::collide(Rect rc)
{
	for (EnemyTank * e : enemies)
	{
		if (e->collide(rc))
		{
			return e;
		}
	}

	return nullptr;
}

void EnemyMgr::stop(double time)
{
	move = false;
	stoptime = time;
	this->time = 0;
}

void EnemyMgr::clear()
{
	for (EnemyTank * e : enemies)
	{
		delete e;
	}

	enemies.clear();
	num = 0;
}

int EnemyMgr::getNum()
{
	return num;
}